package com.mas.resource;

import com.mas.monitor.TickerMonitorResources;

public class ResourceAllocator {

	private String resourceName;

	private double resourceCurrentUsage;

	private double resourcePurchased;

	private double avgResourceCurrentUsage;

	private double maxAllocationValue;
/**
 * constructor for initialing without purchased resource.
 * @param resourceName
 * @param resourceCurrentUsage
 * @param AVGresourceCurrentUsage
 * @param maxAllocationValue
 */
	public ResourceAllocator(String resourceName, double resourceCurrentUsage,
			double AVGresourceCurrentUsage, double maxAllocationValue) {
		this(resourceName, resourceCurrentUsage, AVGresourceCurrentUsage,
				maxAllocationValue, 0);

	}

	/**
	 * constructor for initialing with purchased resource.
	 * Should be used only with TickerMonitor.
	 * @see TickerMonitorResources
	 * @param resourceName
	 * @param resourceCurrentUsage
	 * @param maxAllocationValue
	 * @param resourcePurchased
	 */
	public ResourceAllocator(String resourceName, double resourceCurrentUsage,
			double AVGresourceCurrentUsage, double maxAllocationValue,
			double resourcePurchased) {
		super();
		this.resourceName = resourceName;
		this.resourceCurrentUsage = resourceCurrentUsage;
		this.avgResourceCurrentUsage = AVGresourceCurrentUsage;
		this.maxAllocationValue = maxAllocationValue;
		this.resourcePurchased = resourcePurchased;
	}

	/**
	 * @return available of the resource
	 */
	public double getResourceAvailable() {
		return maxAllocationValue - resourceCurrentUsage - resourcePurchased;
	}

	public boolean allocateResource(double sizeAllocateRequest) {
		// more than can
		if (sizeAllocateRequest + resourceCurrentUsage + resourcePurchased > maxAllocationValue)
			return false;

		resourcePurchased += sizeAllocateRequest;
		return true;
	}

	public boolean releaseResource(double sizeReleaseRequest) {
		resourcePurchased -= sizeReleaseRequest;
		if (resourcePurchased >= 0)
			return true;

		resourcePurchased = 0;
		return false;
	}

	/**
	 * @return the resourceName
	 */
	public String getResourceName() {
		return resourceName;
	}

	/**
	 * @return the resourceCurrentUsage
	 */
	public double getResourceCurrentUsage() {
		return resourceCurrentUsage;
	}

	/**
	 * @return the resourceCurrentUsage
	 */
	public double getResourcePurchased() {
		return resourcePurchased;
	}

	/**
	 * @return the resourceCurrentUsage
	 */
	public double getAvgResourceCurrentUsage() {
		return avgResourceCurrentUsage;
	}

	/**
	 * @return the maxAllocationValue
	 */
	public double getMaxAllocationValue() {
		return maxAllocationValue;
	}

	/**
	 * toString method: creates a String representation of the object
	 * 
	 * @return the String representation
	 */
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		String EOL = "\n";
		buffer.append("ResourceAllocator[");
		buffer.append("resourceName = ").append(resourceName).append(EOL);
		buffer.append("\t\t\t resourceCurrentUsage = ").append(
				resourceCurrentUsage).append(EOL);
		buffer.append("\t\t\t resourcePurchased = ").append(resourcePurchased)
				.append(EOL);
		buffer.append("\t\t\t avgResourceCurrentUsage = ").append(
				avgResourceCurrentUsage).append(EOL);
		buffer.append("\t\t\t maxAllocationValue = ")
				.append(maxAllocationValue).append(EOL);
		buffer.append(" \t ]").append(EOL);
		return buffer.toString();
	}

}
